package com.example.mqtt.thread;

import com.alibaba.fastjson.JSON;
import com.example.mqtt.cache.HistoryJsonCache;
import com.example.mqtt.client.MqttClientConnect;
import com.example.mqtt.entity.properties.MQTTProperties;
import com.example.mqtt.service.RowingService;
import com.example.mqtt.tools.SlurryTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ScheduleTrainData implements Runnable {
    private  String rowingId;

    public void setRowingId(String rowingId) {
        this.rowingId = rowingId;
    }

    private final MqttClientConnect mqttClientConnect = MqttClientConnect.getInstance();

    private final MQTTProperties mqttProperties;
    private final RowingService rowingService;

    public ScheduleTrainData( MQTTProperties mqttProperties, RowingService rowingService) {
        this.mqttProperties = mqttProperties;
        this.rowingService = rowingService;
    }

    @Override
    public void run() {
        mqttClientConnect.sendMessage(SlurryTools.getSplitTopic(mqttProperties.getTrain_data_topic(), rowingService.getRowingById(rowingId).getRowingNo()), JSON.toJSONString(HistoryJsonCache.get(rowingId)));
    }


}
